package com.sl.ssh.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sl.ssh.bean.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getCurrentSession() {
        return this.sessionFactory.openSession();
    }

	@Override
	public List<User> findAll() {
		Session session =getCurrentSession();
		String hql = "from User";
		Query query = session.createQuery(hql);
		return query.list();
	}

	@Override
	public User findByUserName(String userName) {
		Session session =getCurrentSession();
		String hql = "from User where userName = :userName";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		return (User) query.uniqueResult();
	}

}
